package org.sn.jdish.example.queue;

/**
 * 循环队列
 * 
 * @author snzigod@hotmail.com
 *
 */
public class CircleQueue {

	Object[] queue;
	int front;
	int rear;

	public int size = 0;

	public CircleQueue(int size) {
		queue = new Object[size];
		front = 0;
		rear = 0;
	}

	public void enQueue(Object obj) {
		if ((rear + 1) % queue.length == front) {
			return;
		}
		queue[rear] = obj;
		rear = (rear + 1) % queue.length;
		size++;
	}

	public Object deQueue() {
		if (rear == front) {
			return null;
		}
		Object obj = queue[front];
		front = (front + 1) % queue.length;
		size--;
		return obj;
	}

	public boolean isEmpty() {
		return rear == front;
	}

	public static void main(String[] args) {
		int length = 3;
		CircleQueue cq = new CircleQueue(length);
		for (int i = 0; i < length; i++) {
			cq.enQueue(i);
		}
		for (int i = 0; i < length; i++) {
			System.out.println(cq.deQueue());
		}
	}
}
